var store_id,ws,order_sn,member_id,member_avatar,spage='';
var app = angular.module('app',[]);
app.controller('appcontroller',function($scope,$http){
	$scope.initView = function($store_id){
		$http({
			method:"post",
			url:apiRoot + "?act=seller_login&op=store_member",
			data:{
				store_id:$store_id,
				type:'query',
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
                    
             }
		}).then(function SuccessCallback(data){ 
			console.log(JSON.stringify(data));
			if(data.data.code == 200){
            	$scope.m = data.data.datas;
            	$scope.userpic = userpic; 
            	if(data.data.datas.member_sex == '1'){
					var sex = '男';
				}else if(data.data.datas.member_sex == '2'){
					var sex = '女';
				}else{ 
					var sex = '请选择...';
				}
				$scope.sex = sex;
            	
            }else{
            	toast(data.datas.error);
            }
//			$scope.order_list = data.data.datas.order_list; 
//			$scope.pics = pics;  
		},function errorfunction(e){
			error();
		}) 
	}
	$scope.preservation = function(){
//	  	if($("#j_gender").val() == -1){
//        mui.toast("请选择性别！");
//        return;
//      }
		var member_ages = $('.member_ages').val();
		var member_sex = $('.member_sex').val();
		var store_name = $('.store_name').val();
		if(member_sex == '女'){
			var member_sex = 2;
		}else if(member_sex == '男'){
			var member_sex = 1;
		}
		console.log(member_ages+'--'+member_sex+'--'+store_name+'--'+member_avatar);
		$http({
			method:"post",
			url:apiRoot + "?act=seller_login&op=store_member",
			data:{
				store_id:store_id,
				member_id:member_id,
				member_ages:member_ages,
				member_sex:member_sex,
				store_name:store_name,
				member_avatar:member_avatar,
				type:'modify',
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
                    
             }
		}).then(function SuccessCallback(data){ 
			console.log(JSON.stringify(data));
			if(data.data.code == 200){
            	toast('修改成功！');
            	plus.storage.setItem('store_name', store_name + '');
            	if(member_avatar){
					plus.storage.setItem('avatar', member_avatar + '');
            	}
            	plus.webview.getWebviewById(spage).reload();  	//刷新上一个页面 
//          	setTimeout(function(){         //关闭当前页面
//          		ws.close();
//          	},1500)
            }else{
            	toast(data.datas.error);
            }
//			$scope.order_list = data.data.datas.order_list; 
//			$scope.pics = pics;  
		},function errorfunction(e){
			error();
		}) 
	}
	
	
	
})


document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	store_id = plus.storage.getItem('storeid');
	member_id = plus.storage.getItem('member_id');
	aboutElement = document.querySelector('[ng-controller=appcontroller]');
	$scope = angular.element(aboutElement).scope();
	$scope.initView(store_id);
	$scope.$apply();
	
	
	//上传头像
	$('.userpic').on('click',function(){
		plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]},function(e){
			if(e.index == 1){
				var car =plus.camera.getCamera();
				car.captureImage(function(path){
					//展示图片
//					alert(plus.io.convertLocalFileSystemURL(path));
//					$('.shangchuan').attr('src','file://' + plus.io.convertLocalFileSystemURL(path));
					appendpic(path);
					
				},function(err){});
				
			}else if(e.index == 2){
				plus.gallery.pick(function(path){
//					$('.shangchuan').attr('src',path);
					appendpic(path);
				});
			}
		});
	})
	
},false)
//添加图片
var index = 1;
var files = [];
function appendpic(p){
	plus.nativeUI.showWaiting('图片处理中...');
//		console.log(p); 
	//创建新的路径
//				var newpath = p.replace(/\./g , new Date().gettime() + '.');
	var newpath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
			src : p,
			dst : newpath,
			quality : 20
		},
		function(){//毁掉成功
			files.push({name : "uploadkey" + index,path:p});
			index++;
			upload();
		},
		function(error){
		alert('压缩图片失败');
		plus.nativeUI.closeWaiting();
	})
}

function upload(){
	if(files.length <= 0){
		toast("请上传头像！");
	}
//				console.log(JSON.stringify(files));return; 
	var server = apiRoot + "?act=member_data&op=uplodepic&member_id="+member_id;  
	var task = plus.uploader.createUpload(server,
	    {method:"post"},    
		function(t,status){ //上传完成
			if(status == 200){ 
				plus.nativeUI.closeWaiting();
//					console.log(123);
				var infopics = $.parseJSON(t.responseText);
//					console.log(JSON.stringify(infopics));
				if(infopics.code == 200){
					member_avatar = infopics.datas.file_name;
					$('.userpic').attr('src',userpic + infopics.datas.file_name);
				}else{
					toast(infopics.datas.error);
				}
//					plus.nativeUI.toast('处理完成' + t.responseText);
			}else{
				toast('上传失败：' + status);
				plus.nativeUI.closeWaiting();
			}
			
		},function () {
			plus.nativeUI.closeWaiting();
		}
		
	); 
	for(var i = 0;i　<　files.length ; i++){
		var f =files[i];
		task.addFile(f.path,{key:'file'});
//					    console.log(f.name)
	}
	task.start();
	 
}		